package com.mqtt.config;


import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;

/**
 * <p>
 *  mqtt消费端
 * </p>
 *
 * @author gzj
 * @since 2023/6/21
 */

@Configuration
@IntegrationComponentScan
@Slf4j
public class MqttInboundConfiguration {

    @Autowired
    private MqttConfig mqttProperties;


    @Bean
    public MessageChannel mqttInputChannel() {
        return new DirectChannel();
    }

    @Bean
    public MqttPahoClientFactory mqttClientFactory() {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        String[] array = mqttProperties.getHostUrl().split(",");
        MqttConnectOptions options = new MqttConnectOptions();
        options.setServerURIs(array);
        options.setUserName(mqttProperties.getUsername());
        options.setPassword(mqttProperties.getPassword().toCharArray());
        options.setKeepAliveInterval(mqttProperties.getKeepalive());
        options.setConnectionTimeout(mqttProperties.getTimeout());
        options.setAutomaticReconnect(true);
        options.setCleanSession(false);

        
        factory.setConnectionOptions(options);
        return factory;
    }

    //配置client,监听的topic
    @Bean
    public MessageProducer inbound() {
        // 修复华为云IoTDA订阅主题格式
        String[] inboundTopics = mqttProperties.getDefaultTopic().split(",");
        MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(
                mqttProperties.getClientId(), mqttClientFactory(), inboundTopics);
        adapter.setCompletionTimeout(mqttProperties.getTimeout());
        adapter.setRecoveryInterval(5000);
        adapter.setQos(1);
        adapter.setConverter(new DefaultPahoMessageConverter());
        adapter.setOutputChannel(mqttInputChannel());

        return adapter;
    }




    //通过通道获取数据
    @Bean
    @ServiceActivator(inputChannel = "mqttInputChannel")  //异步处理
    public MessageHandler handler() {
        return new MessageHandler() {
            @Override
            public void handleMessage(Message<?> message) throws MessagingException {
//				log.info("message:"+message);
                log.info("----------------------");
                log.info("----------------------");
                log.info("message:"+message.getPayload());
                log.info("PacketId:"+message.getHeaders().getId());
                log.info("Qos:"+message.getHeaders().get(MqttHeaders.QOS));
                String topic = (String) message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC);
                log.info("topic:"+topic);
            }
        };
    }
    
}
